# -*- coding: utf-8 -*-
import random
import requests
import json
import xlsxwriter
import time
from fake_useragent import UserAgent
ua = UserAgent()

refer_url = 'http://125.35.6.80:8181/ftban/fw.jsp'
base_url = "http://125.35.6.80:8181/ftban/itownet/fwAction.do"
ajax_url = 'http://125.35.6.80:8181/ftban/itownet/fwAction.do?method=getBaNewInfoPage'
base_detail_url = 'http://125.35.6.80:8181/ftban/itownet/hzp_ba/fw/pz.jsp'
page = 1
querystring = {"method": "getBaInfo"}
user_agents = [
    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
]
# first request data
data = {
    'on': 'true',
    'pageSize': 50,
    'productName': '',
    'condition Type': 1,
    'applyname': '',
    'applysn': ''
}

headers1 = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Length': '46',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Host': '125.35.6.80:8181',
    'Origin': 'http://125.35.6.80:8181',
    'Referer': refer_url,
    #'User-Agent': user_agents[random.randrange(0, 4)],
    'User-Agent': ua.random,
    'X-Anit-Forge-Code': '0',
    'X-Anit-Forge-Token': 'None',
    'X-Requested-With': 'XMLHttpRequest',
}

headers2 = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Length': '46',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Host': '125.35.6.80:8181',
    'Origin': 'http://125.35.6.80:8181',
    'Referer': 'http://125.35.6.80:8181/ftban/itownet/hzp_ba/fw/pz.jsp',
    'User-Agent': user_agents[random.randrange(0,4)],
    'X-Anit-Forge-Code': '0',
    'X-Anit-Forge-Token': 'None',
    'X-Requested-With': 'XMLHttpRequest',
    'Postman-Token': "bfaee581-103b-41d0-ba1c-691a9bb1f375"
}
headers3 = {
    'Content-Type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
    'User-Agent': ua.random,
    'Postman-Token': "bfaee581-103b-41d0-ba1c-691a9bb1f375"
    }

# 获取实际生产企业
def getEnterprise(json):
    enterprise = "企业名称："
    enterprise = enterprise + json["enterprise_name"]
    enterprise = enterprise + "     企业地址：" + json["enterprise_address"]
    enterprise = enterprise + "     生产许可号：" + json["enterprise_healthpermits"]
    return enterprise

# 获取备注
def getRemark(lists):
    content = ""
    for idx in range(len(lists)):
        content = lists[idx]["checkDate"]
        content = content + " 对\"" + lists[idx]["unitName"] + "\"进行了备案后检查"
        if(lists[idx]["check_type"] != "通过"):
            content = content + "，检查结果：" + lists[idx]["check_type"]
            content = content + " 原因： " + lists[idx]["remark"]
        content = content + "\n"
    return content

# 获取成分
def getComponents(lists):
    components = ""
    if(len(lists)>0):
        components = lists[0]["pfname"] + ":"
    for idx in range(len(lists)):
        components = components + lists[idx]["cname"] + ";"
    return components

# get page detail by url
def getDetailHtml(payload, product):
    try:
        print(payload)
        response = requests.request("POST", base_url, data=payload, headers=headers3, params=querystring)
        time.sleep(2)
        result = response.json()
        print(response.text)
        # 企业地址
        address = "生产企业地址    " + result["scqyUnitinfo"]["enterprise_address"]
        explain = result["remark"]
        product["enterpriseAddress"] = address
        product["explain"] = explain
        product["components"] = getComponents(result["pfList"])
        product["remark"] = getRemark(result["ck"])
        product["enterprise"] = getEnterprise(result["sjscqyList"][0])
        print(product)
        return product
    except requests.HTTPError as e:
        # code / reason / headers 异常
        print('get html httperror:{}'.format(e))
        return ''

# 存Excel
def writeExcel(worksheet, product, row):
    worksheet.write_string(row, 0, product["productName"])
    worksheet.write_string(row, 1, product["applySn"])
    worksheet.write_string(row, 2, product["provinceConfirm"])
    worksheet.write_string(row, 3, product["enterpriseName"])
    worksheet.write_string(row, 4, product["enterpriseAddress"])
    worksheet.write_string(row, 5, product["enterprise"])
    worksheet.write_string(row, 6, product["components"])
    worksheet.write_string(row, 7, product["explain"])
    worksheet.write_string(row, 8, product["remark"])

def run(worksheet):
    # 112536
    for pageNum in range(1, 33761):
        data['page'] = pageNum
        resp = requests.post(ajax_url, data=data, headers=headers1)
        print(resp)
        #time.sleep(1)
        result = resp.json()
        product_list = result['list']
        for i in range(0, 1):
            product = {}
            prod = product_list[i]
            product["applySn"] = prod['applySn'] # 备案
            product["enterpriseName"] = prod['enterpriseName']  # 企业名字
            processid = prod['processid']
            product["productName"] = prod['productName']  #产品名
            product["provinceConfirm"] = prod['provinceConfirm'] #注册时间

            payload = "processid="+ processid +"&j_captcha=null"
            product = getDetailHtml(payload, product)

            writeExcel(worksheet, product, (pageNum-1)*15 + i+1)
        print("当前进度%d%", (pageNum/112536)*100)


if __name__ == "__main__":
    workbook = xlsxwriter.Workbook('res_data.xlsx')
    worksheet = workbook.add_worksheet()

    # 设定格式，等号左边格式名称自定义，字典中格式为指定选项
    # bold：加粗，num_format:数字格式
    bold_format = workbook.add_format({'bold': True})
    # money_format = workbook.add_format({'num_format': '$#,##0'})
    # date_format = workbook.add_format({'num_format': 'mmmm d yyyy'})

    # 用符号标记位置，例如：A列1行
    worksheet.write('A1', '产品名', bold_format)
    worksheet.write('B1', '备案编号', bold_format)
    worksheet.write('C1', '备案日期', bold_format)
    worksheet.write('D1', '生产企业', bold_format)
    worksheet.write('E1', '生产企业地址', bold_format)
    worksheet.write('F1', '实际生产企业', bold_format)
    worksheet.write('G1', '成分', bold_format)
    worksheet.write('H1', '说明', bold_format)
    worksheet.write('I1', '备注', bold_format)
    run(worksheet)
    workbook.close()